<!--#include file="config.asp"-->
<!--#include file="json.asp"-->
<!--#include file="conn.asp"-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<%
'================================================================
'=   文件名称：access_tocken.asp                                =
'=   实现功能：根据code获取access_tocken openid                 =
'=   QQ and Wx：676750993                                       =
'================================================================  

	code=request("code")
	ip=request("ip")
	'response.write(code)
	
	oauthUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="&AppID
	oauthUrl = oauthUrl & "&secret="&AppSecret
	oauthUrl = oauthUrl & "&code="& code &"&grant_type=authorization_code"
	
	set objXmlHttp=Server.CreateObject("MSXML2.XMLHTTP")   
  
  objXmlHttp.open "GET",oauthUrl,false
  objXmlHttp.send()
  
  resultText=objXmlHttp.responseText
   
  set objXmlHttp=nothing
  
  '解析json access_tocken tocken值,expire_in 有效期 单位秒 7200
  '{
  '"access_token":"ACCESS_TOKEN",
  '"expires_in":7200,
  '"refresh_token":"REFRESH_TOKEN",
  '"openid":"OPENID",
  '"scope":"SCOPE" 
  '}
  'response.write resultText
  Set res = parseJSON(resultText) 
  openid = res.openid
  access_token = res.access_token
  
  sql="select * from userinfo where ip='"&ip&"'"
  set rs=server.CreateObject("adodb.recordset")
	rs.open sql,conn
	if rs.eof then
		rs.close
		set rs=nothing
		response.write "<script>alert('扫码失败，请刷新电脑二维码页重新扫码');</script>"		
		response.end
	end if
	
	rs.close
	set rs=nothing 
	
	set rsUp=server.CreateObject("adodb.recordset")
	sql="update userinfo set openid='"& openid &"',access_token='"& access_token &"',login_state='1' where ip='"& ip &"'"
	
	rsUp.open sql,conn
	on error resume next
	rsUp.close
	set rsUp = nothing
	
	response.write "<script>alert('扫码成功，请查看电脑页是否成功');</script>"
	
  
%>